package com.activiti.jerry.service;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.web.client.RestTemplate;

public class pipeService {

	private RestTemplate restClient = new RestTemplate();
	
	private ObjectMapper jsonMapper = new ObjectMapper();
	
	
	public int getFile(/*String fileId*/){
		String fileId = "3c41fc5940a34832af0b62cb95ec78c9";
		String reqUrl = "http://upload.iqiyi.com/fileinfo?file_id={file_id}";
		Map<String, String> params = new HashMap<String, String>();
		params.put("file_id", fileId);
		try{
			String resp = this.restClient.getForObject(reqUrl, String.class, params);
			JsonNode jsonNode = this.jsonMapper.readTree(resp);
			String code = jsonNode.get("code").getValueAsText();
			JsonNode data = null;
			if(StringUtils.equals(code, "A00000")){
				data = jsonNode.get("data");
				if(data != null){
					System.out.println("filesize: "+ data.get("filesize").getValueAsLong());
					System.out.println("status: "+ data.get("status").getValueAsInt());
					System.out.println("cloud path: " + data.get("path").getValueAsText());
					return 1;
				}else{
				    System.out.println("no data in fileid: " + fileId);	
				}
			}else{
				System.out.println("can not get file: "  + fileId);
			}
		}catch(Exception e){
			System.out.println("exception happened when get file, exception: " + e.getMessage());
		}
		return 2;
	}
}